package cn.yunhe.spring.struts.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import org.apache.struts2.ServletActionContext;

import java.io.PrintWriter;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/1.
 * 如果接受的值为空则不往下执行
 */
public class SessionInterceptor extends MethodFilterInterceptor {
    @Override
    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
        System.out.println("会话拦截器现在开始》》》》》》");
        //得到会话session，通过seesion拦截某些方法的功能
        Map<String,Object> session=actionInvocation.getInvocationContext().getSession();

        if (session.get("login")==null){
            PrintWriter out= ServletActionContext.getResponse().getWriter();
            out.println("-1");
            out.flush();
            out.close();
            return "nologin";
        }
        //System.out.println("会话拦截器现在开始》》》》》》");
        String result=actionInvocation.invoke();
        System.out.println("会话拦截器拦截结束》》》》》》");
        return result;
    }
}
